package com.example.administrator.familfinalcialmanagement.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.User;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityRegisterBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.UserDao;
import com.example.administrator.familfinalcialmanagement.ui.view.RegisterTime;
import com.example.administrator.familfinalcialmanagement.util.ApiHTTP;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/26.
 */

public class RegisterActivity extends BaseActivity {

    ActivityRegisterBinding binding;
    RegisterTime timeUtil;
    String bodyType;

    UserDao userDao = GreenDaoUtils.getSingleTon().getmDaoSession().getUserDao();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        setBack();
        setTitleText("注册");
        bodyType = getIntent().getStringExtra("module_type");
        binding.textViewUpdata.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeUtil != null) {
            timeUtil.onCancel();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textViewUpdata:
                String phone = binding.etModulePhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入手机号码");
                    return;
                }

                String pwd = binding.etModulePwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入密码");
                    return;
                }

                String pwdAgain = binding.etModulePwdAgain.getText().toString().trim();
                if (TextUtils.isEmpty(pwdAgain)) {
                    ToastUtil.showToast("请再次输入密码");
                    return;
                }

                if (!pwdAgain.equals(pwd)) {
                    ToastUtil.showToast("两次密码输入不一致请重新输入");
                    binding.etModulePwdAgain.setText("");
                    return;
                }

                String classname = binding.etModuleClass.getText().toString().trim();
                if (TextUtils.isEmpty(classname)) {
                    ToastUtil.showToast("请输入倾向课程");
                    return;
                }

                User user = new User();
                user.setName(phone);
                user.setPwd(pwd);
                user.setOne(classname);
                switch (bodyType) {
                    case "1":
                        user.setJob("teacher");
                        userDao.insert(user);
                        break;
                    case "2":
                        user.setJob("student");
                        userDao.insert(user);
                        break;
                    default:
                }
                finish();
                break;
            default:
        }
    }
}
